package com.example.littleSpider.myapplication.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.example.littleSpider.myapplication.R;
import com.example.littleSpider.myapplication.adapter.BuyingSpreeAdapter;
import com.example.littleSpider.myapplication.adapter.ShoppingCartAdapter;
import com.example.littleSpider.myapplication.base.BaseActivity;
import com.example.littleSpider.myapplication.bean.ShoppingCartBean;
import com.example.littleSpider.myapplication.myInterfaces.CheckAllListener;
import com.example.littleSpider.myapplication.myInterfaces.MethodInTitle;
import com.example.littleSpider.myapplication.view.TitleView;
import com.littlespider.mylibrary.util.T;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 特卖模块购物车点击进入的界面
 * Created by aduser on 2017/12/12.
 */

public class ShoppingCartActivity extends BaseActivity implements MethodInTitle, CheckAllListener{

    @BindView(R.id.title_view)
    TitleView mTitleView;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.expand_view)
    ExpandableListView expand_view;
    @BindView(R.id.tv_close_an_account)
    TextView tv_close_an_account;
    @BindView(R.id.ck_check_all)
    CheckBox checkAll;

    private boolean isCheckedAll = false;
    List<List<ShoppingCartBean>> lists = new ArrayList<>();
    List<Boolean> isCheckedList = new ArrayList<>();
    List<ShoppingCartBean> groupList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_shopping_cart;
    }

    @Override
    protected void initView() {
        mTitleView.initMyTitle(getString(R.string.shopping_cart), this, false);
        mTitleView.hideTitleTextImage();
        final List<String> list = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            list.add("");
        }
        for (int i = 0; i < 3; i++) {
            List<ShoppingCartBean> datas = new ArrayList<>();
            for (int j = 0; j < i + 3; j++) {
                ShoppingCartBean bean = new ShoppingCartBean();
                bean.setChoose(false);
                datas.add(bean);
            }
            lists.add(datas);
            isCheckedList.add(false);
            ShoppingCartBean bean = new ShoppingCartBean();
            bean.setChoose(false);
            groupList.add(bean);
        }
        mRecyclerView.setAdapter(new BuyingSpreeAdapter(R.layout.item_shopping_cart, list));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//        final MyAdapter myAdapter = new MyAdapter();
        final ShoppingCartAdapter cartAdapter = new ShoppingCartAdapter(this, this, groupList, lists);
//        expand_view.setAdapter(myAdapter);
        expand_view.setAdapter(cartAdapter);
        expand_view.setGroupIndicator(null);
        for (int i = 0; i < lists.size(); i++) {
            expand_view.expandGroup(i);
        }

        checkAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                cartAdapter.checkAll(isCheckedAll);
                isCheckedAll = !isCheckedAll;
                for (int i = 0; i < lists.size(); i++) {
                    ShoppingCartBean bean = new ShoppingCartBean();
                    bean.setChoose(isCheckedAll);
                    groupList.set(i, bean);
                    isCheckedList.set(i,isCheckedAll);
                    for (int j = 0; j < lists.get(i).size(); j++) {
                        lists.get(i).get(j).setChoose(isCheckedAll);
                    }
                }
                cartAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tv_close_an_account)
    public void pay(){
        T.show(this, "结算.");
        tv_close_an_account.setClickable(false);
    }

    @Override
    public void setGoBack() {
        finish();
    }

    @Override
    public void onRightOne() {
        Intent intent = new Intent(this, MyEspeciallySaleActivity.class);
        startActivity(intent);
    }

    @Override
    public void onRightTwo() {

    }

    @Override
    public void onTitleText() {

    }

    @Override
    public void selectorAll() {
        isCheckedAll = true;
        checkAll.setChecked(true);
    }

    @Override
    public void selectorNotAll() {
        isCheckedAll = false;
        checkAll.setChecked(false);
    }

}
